package com.jh.ul;

import Dao.MysqlOperate;
import JDBC.ConnDatabase;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

public class Login extends JFrame implements ActionListener {
    //登录界面
    //实现登录简单的登录功能

    JTextField username =null;
    JPasswordField password =null;
    //登录按钮
    JButton jButton1 =null;
    //注册按钮
    JButton jButton2=null;

    //登录权限
    public static final int manager_per=1;
    public static final int user_per=0;


    //首层
    public Login() {
        //窗口初始化，按钮、单选框、输入框、监听、跳转
        setJFrame();
        //连接数据库
        conndatabse();
        //判断是否可以登录，和权限
        Login_password();

        this.setVisible(true);
    }

    private void Login_password() {
    }

    private void conndatabse() {
    }



    private void setJFrame(){
        this.setSize(380,360);
        this.setTitle("登录界面");

        //固定尺寸
        this.setResizable(false);

//        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        //取消组件默认布局
        this.setLayout(null);

        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //图片加载
        username = new JTextField();
        username.setBounds(132,122,170,22);
        this.getContentPane().add(username);

        password = new JPasswordField();
        password.setBounds(132,170,170,22);
        this.getContentPane().add(password);

        jButton1 = new JButton(new ImageIcon("resource\\登录按钮.jpg"));
        jButton1.setBounds(80,240,87,40);
//        jButton1.setFocusPainted(false);
//        jButton1.setBackground( Color.decode("#ffe9c0"));
        this.getContentPane().add(jButton1);


        jButton2 = new JButton(new ImageIcon("resource\\注册按钮.jpg"));
        jButton2.setBounds(200,240,87,40);
//        jButton2.setFocusPainted(false);
        this.getContentPane().add(jButton2);

        JLabel jLabel = new JLabel(new ImageIcon("resource\\登录界面.jpg"));
        jLabel.setBounds(0,0,380,360);
        this.getContentPane().add(jLabel);

        //鼠标监听
        jButton1.addActionListener(this);
        jButton2.addActionListener(this);


    }


    @Override
    public void actionPerformed(ActionEvent e) {
        Object source=e.getSource();
        //获取填入的用户信息
        String user_name = username.getText();
        String user_password = String.valueOf(password.getPassword());



        if (source==jButton2){
            //用户注册
            this.setVisible(false);
            new User_Register();
        }else if (source==jButton1){
            //登录
            //读取数据库，进行信息对比
            ArrayList<Object[]> arr_data=null;
            try {
               arr_data=MysqlOperate.find("user_info",null,null);
            } catch (SQLException | ClassNotFoundException ex) {
                throw new RuntimeException(ex);
            }
            for (int i = 0; i < arr_data.size(); i++) {
                Object[] temp= arr_data.get(i);

                if (temp[0].equals(user_name)&&temp[1].equals(user_password)){
                    //登录成功，判断登录权限
                    if(temp[2].equals(manager_per)){
                        this.setVisible(false);
                        new admin_Frame();
                    }else if (temp[2].equals(user_per)) {
                        this.setVisible(false);
                        new Selection_Frame();
                    }else {
                        new error("非法权限");
                    }

                }

            }

        }
    }
}
